﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNALibrary;
using Netpong.Networking;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;

namespace NetPong.PongClasses3d
{
    public class Ball3d: Ball, IRenderable, IWatchable
    {
        public Body3d rigidBody;

        public Ball3d(Vector2 pos, NetworkManager nm, int rad, PhysicsSimulator ps, ContentManager cm, InputHandler ih)
            : base(nm, pos, rad, ps, ih)
        {
            Model model = cm.Load<Model>(".\\Content\\Models\\sphere");
            rigidBody = new Body3d(model, GetBody);

            rigidBody.elevation = rad + 3;
            rigidBody.Scale = 1f * rad;
        }

        public Ball3d(Vector2 pos, NetworkManager nm, int rad, PhysicsSimulator ps, ContentManager cm) : this(pos, nm, rad, ps, cm, null) { }

        public Body GetBody()
        {
            return body;
        }

        public override void RenderDefault(RenderControl rc, GameTime gt)
        {
            rigidBody.Rotate(0f, body.LinearVelocity.Y / radius / 2, -1 * body.LinearVelocity.X / radius / 2);
            rigidBody.Render(rc, gt);
        }

        public override void RenderAnimation(RenderControl rc, GameTime gt)
        {
            throw new NotImplementedException();
        }

        public override void Update()
        {
            base.Update();
            if (rigidBody.effect != null)
                rigidBody.effect.Update();
        } 

        #region IWatchable Members

        public Vector3 GetPosition()
        {
            return rigidBody.Position;
        }

        public Vector3 GetDirection()
        {
            return Vector3.Forward;
        }

        public Vector3 GetUp()
        {
            return Vector3.Up;
        }

        #endregion
    }
}
